var spd = 300,espd=1000;
var dir;
var s1, s2;
var i = 5, j = 0;
var ii = 5, jj;
var ex = 0, ey;
var score = 0;
var instruction = "按左右方向键控制飞机，空格键发射";

function tab() {
    var tx = "";
    tx = "<table>"
    for (var x = 0; x < 6; x++) {
        tx += "<tr>";
        for (var y = 0; y < 6; y++) {
            tx += "<td id=b" + x + y + "></td>";
        }
        tx += "</tr>";
    }
    tx += "</table>"
    document.getElementById("pr").innerHTML = tx;
    plane();
    document.getElementById("bid").innerHTML = instruction;
    enemy();
    enemymov()
    s2 = setInterval("shootin()", 10);
}
window.onload = tab;

function run() {
    dir = event.keyCode;
    moved(i, j);
    if (dir == 37) {
        j--;
    }
    if (dir == 39) {
        j++;
    }
    if (dir == 32) {
        posx();
        bullet();
    }
    i = edge(i);
    j = edge(j);
    move(i, j);
    document.getElementById("bid").innerHTML = instruction;

}
//子弹坐标初始化
function posx() {
    ii = 5;
    jj = j;
}

//飞机
function plane() {
    //document.getElementById("b" + i + j).style.backgroundColor = "#D8BFD8"
    document.getElementById("b" + i + j).style.backgroundImage = "url(1.jpg)";
}
//子弹
function bullet() {
    //document.getElementById("b" + ii + jj).style.backgroundColor = "";
    document.getElementById("b" + ii + jj).style.backgroundImage = "";
    plane();
    ii--;
    //document.getElementById("b" + ii + jj).style.backgroundColor = "#EEA2AD";
    document.getElementById("b" + ii + jj).style.backgroundImage = "url(2.jpg)";

    s1 = setTimeout("bullet()", spd);
}
//飞机左右范围
function edge(c) {
    if (c < 0) {
        //alert("边界");
        return c = 0;
    } else
        if (c > 5) {
            //alert("边界");
            return c = 5;
        } else {
            return c;
        }
}
//飞机
function move(a, b) {
    //document.getElementById("b" + a + b).style.backgroundColor = "#EEA2AD";
    document.getElementById("b" + i + j).style.backgroundImage = "url(1.jpg)";
}
//清除上一个位置飞机
function moved(a, b) {
    //document.getElementById("b" + a + b).style.backgroundColor = "";
    document.getElementById("b" + i + j).style.backgroundImage = "";
}
//敌机
function enemy() {
    ex = Math.floor(Math.random() * 3);
    //ex++;
    ey = Math.floor(Math.random() * 6);
    //document.getElementById("b" + ex + ey).style.backgroundColor = "#EEA2AD";
    document.getElementById("b" + ex + ey).style.backgroundImage = "url(3.jpg)"
}
//敌机移动
function enemymov() {
    document.getElementById("b" + ex + ey).style.backgroundImage = ""
    ex++;
    document.getElementById("b" + ex + ey).style.backgroundImage = "url(3.jpg)"
    setTimeout("enemymov()", espd);
    if(score==300){
        espd=500;
       document.getElementById("totle").innerHTML = i + "," + j + ";" + "得分：" + score + ";达到 300分敌机加速了";
    }
}
//打中敌机
function shootin() {
    if (ii == ex && jj == ey) {
        //document.getElementById("b" + ex + ey).style.backgroundColor = "#BBA2AD";
        //document.getElementById("b" + ex + ey).style.backgroundColor = "";
        document.getElementById("b" + ex + ey).style.backgroundImage = "url(4.jpg)"
        score += 10;
        document.getElementById("totle").innerHTML = i + "," + j + ";" + "得分：" + score;
        ex = 0;
        enemy();
    }
  
    if (ex > 5||(i==ex&&j==ey)) {
        //document.getElementById("pr").style.display = "none";
        document.onkeydown=null;
        document.getElementById("totle").innerHTML = i + "," + j + ";" + "得分：" + score + ";Game Over";
    }
    if (i==ex&&j==ey){
     document.getElementById("b" + ex + ey).style.backgroundImage = "url(4.jpg)"
     document.onkeydown=null;
     document.getElementById("totle").innerHTML = i + "," + j + ";" + "得分：" + score + ";Game Over";
    }
}
document.onkeydown = run;